package mess;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class LeeCode_202 {
    public static void main(String[] args) {
        System.out.println(isHappy(2));
    }

    private static boolean isHappy(int n) {
        Set<Integer> used = new HashSet<>();
        return backTrick(used, n);
    }

    private static boolean backTrick(Set<Integer> used, int n) {
        int sum = 0;
        while (n != 0) {
            int single = n % 10;
            n /= 10;
            sum += single * single;
        }
        if (sum == 1)
            return true;
        else if (used.contains(sum)) {
            return false;
        } else {
            used.add(sum);
            return backTrick(used, sum);
        }
    }
}
